#include "includes.h"
uint8_t winner;
uint8_t point_limit;
uint8_t mplay_ready;
int internalpoints;
int externalpoints;

enum menu_state_enum game_menu_state;

void game_start(enum menu_state_enum menu_state)
{	
	game_menu_state = menu_state;
	game_new();
	switch (menu_state)
	{
		case SINGLEPLAYERPOINTS:  
			game_set_point_limit();
			game_init_single();
			break;
		case SINGLEPLAYERETERNAL: 
			game_init_single();
			break;
		case MULTIPLAYERPOINTS:  
			game_set_point_limit();
			game_init_mplay();
			break;
		case MULTIPLAYERETERNAL:  break;
		default: butterfly_print("Should not come here");
	}
	game_end();
}

void game_add_point(can_message_t* msg)
{

	// Do not keep score if point_limit=0
	if(point_limit>0)
	{
		if(msg->data[0] == GROUP)
		{
			internalpoints++;
			USART_print("internalpoint++");
		}
		else
		{
			externalpoints++;
		}
		// Check if the point limit has been reached.
		if ((internalpoints+1)>point_limit || (externalpoints+1)>point_limit)
		{
			game_winner(msg);	// The source of the message is the looser since he got
			return;				// most points.
		}
	}

	game_display_score();
}

void game_winner(can_message_t* msg)
{	
	char display[10];
	sprintf(display,"Group %d loose",(uint8_t) msg->data[0]);
	butterfly_print(display);
	_delay_ms(3000);
	winner=1;
	game_end();
}

void game_set_point_limit()
{
	uint8_t limit = 10;
	char button = '0',last_button='0';
	char temp[10];
	
	butterfly_print("Select limit");
	_delay_ms(2000);
	while(button!=KEY_ENTER) 
	{
		last_button = button;
		button = butterfly_poll_joystick();
		if(button != last_button) 
		{
            switch (button) 
			{
                case KEY_ENTER: point_limit=limit; break;
	            case KEY_PLUS:  limit++; break;
				case KEY_MINUS: limit--; break;
				case KEY_PREV:	butterfly_menu(); break;
				default: break;
			}
			butterfly_print_noscroll(itoa(limit,temp,10));
		}
	}
}	


void game_mplay_ready()
{
	mplay_ready=1;	
}

void game_new()
{
	winner=0;
	mplay_ready = 0;
	point_limit = 0;
	internalpoints = 0;
	externalpoints = 0;
}

void game_init_mplay()
{
	int counter=10;
	// Joystick structure
	can_message_t joy;
	joy.id = JOY_COORDS;
	joy.length = 3;
	joy.data[0] = GROUP;
	
	// Multiplayer struct
	can_message_t mplay;
	mplay.id = PLAYER_READY;
	mplay.length=1;
	mplay.data[0]=GROUP;

	butterfly_print("Waiting for more players");
	can_message_send(BUFFER0,&mplay);
	while(mplay_ready==0)
	{
		if(counter==0)
		{
			butterfly_menu();
		}	
		else
		{
			can_message_send(BUFFER0,&mplay);
			_delay_ms(1000);
			counter--;
		}
	}
	can_message_send(BUFFER0,&mplay);
	mplay_ready=0;
	// Send game mode and point limit
	if(point_limit == 0 && game_menu_state==MULTIPLAYERETERNAL) 
	{
		// Eternal game
		mplay.id = MULTI_GAME_MODE;
		mplay.data[0] = MULTI_ETERNAL;
		can_message_send(BUFFER0,&mplay);
	}
	else if(game_menu_state==MULTIPLAYERPOINTS)
	{
		mplay.id = MULTI_POINTS;
		if(point_limit==0)
		{
			game_set_point_limit();
			mplay.data[0] = point_limit;
		}
		else
		{
			mplay.data[0] = point_limit;
		}
		
	}
	else
	{
		//Error
		USART_print("unknown gamestate in game_init_mplay");	
	}
	can_message_send(BUFFER0,&mplay);
	_delay_ms(5);
	game_init_single();

}


void game_init_single()
{
	char button='0';
	can_message_t joy;
	joy.id = JOY_COORDS;
	joy.length = 3;
	joy.data[0] = GROUP;

	game_display_score();
	while((button != KEY_ENTER) && winner!=1)
	{  
		button = butterfly_poll_joystick();
		// send x og y
		joy.data[1] = gety();
		_delay_ms(5);
		joy.data[2] = getx();
		_delay_ms(5);
		can_message_send(BUFFER0, &joy);
		_delay_ms(20);
//		wii();

	} 
}

void game_display_score()
{
	char display[10];
	sprintf(display,"%d - %d",internalpoints,externalpoints);
	butterfly_print_noscroll(display);

}

void game_end()
{
	char button='0';
	butterfly_print_noscroll("GAME OVER");
	while(button != KEY_ENTER)
	{
	 	button = butterfly_poll_joystick();
	}
	butterfly_menu();
}

void game_set_game_mode(enum menu_state_enum menu_state)
{
	game_menu_state = menu_state;
}	
